/*
 * @Author: zqx me_zqx@163.com
 * @Date: 2025-02-19 18:11:35
 * @LastEditors: zqx me_zqx@163.com
 * @LastEditTime: 2025-08-29 11:15:25
 * @FilePath: /XMate-h5/src/utils/colorTool.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

// 生成同色系的颜色
export function generateSimilarColor(baseColor) {
  // 将hex转为rgb
  const r = parseInt(baseColor.slice(1, 3), 16);
  const g = parseInt(baseColor.slice(3, 5), 16);
  const b = parseInt(baseColor.slice(5, 7), 16);

  // 随机调整色值，保持在30以内的变化以维持同色系
  const variation = () => Math.floor(Math.random() * 60 - 45);

  // 确保颜色值在0-255范围内
  const clamp = num => Math.min(255, Math.max(0, num));

  const newR = clamp(r + variation());
  const newG = clamp(g + variation());
  const newB = clamp(b + variation());

  // 转回hex
  return (
    "#" + [newR, newG, newB].map(x => x.toString(16).padStart(2, "0")).join("")
  );
}

// 计算颜色的透明度
export function calculateOpacity(baseColor, opacity = 0.1) {
  // 确保透明度在0到1之间
  const clampedOpacity = Math.min(1, Math.max(0, opacity));
  
  // 计算透明度的hex值
  const alphaHex = Math.round(clampedOpacity * 255).toString(16).padStart(2, '0');

  // 返回带透明度的hex颜色
  return `#${baseColor.slice(1)}${alphaHex}`;
}



export function getAvatarColor(name) {
  if (name === undefined || name === null) {
    return "#1677FF";
  }
  // 现代扁平化设计的颜色方案
  const colors = [
    // 主色调
    "#1677FF", // 蓝色
    "#436BFF", // 品牌蓝
    "#4096FF", // 浅蓝

    // 辅助色
    "#52C41A", // 绿色
    "#722ED1", // 紫色
    "#13C2C2", // 青色
    "#2F54EB", // 深蓝
    "#FA8C16", // 橙色

    // 中性色
    "#597EF7", // 淡蓝
    "#36CFC9", // 青绿
    "#9254DE", // 淡紫
    "#40A9FF", // 天蓝
    "#73D13D", // 淡绿
    "#FF7A45", // 珊瑚
    "#FFA940" // 金色
  ];

  // 使用字符串的第一个字符的 Unicode 值作为索引
  const charCode = name.charCodeAt(0);
  return colors[charCode % colors.length];
}


// 常用标签颜色
export const colors21 = [
  '#33C4FF',
  '#04CCA4',
  '#F6BD16',
  '#FF6B3B',
  '#5B8FF9',
  '#FF75AD',
  '#8D7AF2',
  '#289CCB',
  '#1DAC42',
  '#C39611',
  '#CB552E',
  '#4871C6',
  '#CB5D89',
  '#7061C0',
  '#85DBFF',
  '#7DE899',
  '#F9D774',
  '#FFA68A',
  '#9DBCFB',
  '#FFACCE',
  '#BBB7F7'
];

export function getHexColorWithAlpha(color, alpha) {
  if (!color || typeof color !== 'string' || !color.startsWith('#')) {
    throw new Error('Invalid HEX color format');
  }
  if (typeof alpha !== 'number' || alpha < 0 || alpha > 1) {
    throw new Error('Alpha must be a number between 0 and 1');
  }
  const hex = color.replace('#', '').toUpperCase();
  const alphaHex = Math.round(alpha * 255).toString(16).padStart(2, '0').toUpperCase();
  return `#${hex}${alphaHex}`;
}
